import java.io.*;
import java.util.Random;
import java.util.Scanner;

public class Dao {
    public boolean judge(String id){
        String str = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{7}$";
        return id.matches(str);
    }
    public int[] symbol(){
        Random random = new Random();
        int[] ints = new int[10];
        int a;
        for (int i = 0;i<10;i++){
            a = random.nextInt(4);
            ints[i] = a;
            //如果相邻的两个符号相同，循环进行回退
            if (i>0&&ints[i]==ints[i-1]){
                i--;
            }
        }
        return ints;
    }
    //生成题目
    public Problem[] createProblem(){
        Random rd = new Random();
        Problem[] problems = new Problem[10];
        int[] symbol = symbol();
        for (int i = 0;i<10;i++){
            int x = rd.nextInt(99)+1;
            int y = rd.nextInt(99)+1;
            //创建临时问题对象，满足要求后存入数组
            Problem temp = new Problem(x,y,symbol[i]);
            temp.matchSymbol();
            //如果是除法要保证两个整数能整除
            if (symbol[i]==3){
                if (x%y==0){
                    problems[i] = temp;
                }else {
                    i--;
                }
            }else {
                //结果必须保证在100以内
                if (temp.getResult()>0&&temp.getResult()<100){
                    problems[i] = temp;
                }else {
                    i--;
                }
            }
        }
        return problems;
    }
    public void test(Student s) throws IOException {
        long time1 = System.currentTimeMillis();
        int correctNum = 0;
        Scanner sc = new Scanner(System.in);
        Problem[] problems = createProblem();
        for (int i = 0;i<10;i++){
            System.out.print(problems[i].getExpression());
            problems[i].setStudentResult(sc.nextInt());
            if (problems[i].getResult()==problems[i].getStudentResult()){
                correctNum++;
            }
        }
        long time2 = System.currentTimeMillis();
        long time = (time2-time1)/1000;
        //输入用户信息
        s.setTime(time);
        s.setScore(correctNum*10);
        //将用户成绩写入文件record.txt
        FileWriter writer = new FileWriter("D:\\record.txt",true);
        BufferedWriter bw = new BufferedWriter(writer);
        bw.write(s.toString());
        bw.write("\n");
        bw.flush();          //将缓冲区的内容写入
        //将题目信息写入文件problem
        FileWriter fileWriter = new FileWriter("D:\\problem.txt",true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        bufferedWriter.write("\n");
        for (int i = 0;i<10;i++){
            bufferedWriter.write(problems[i].toString());
            bufferedWriter.newLine();
            bufferedWriter.flush();
        }
    }
    //检查分数
    public void check() throws IOException {
        FileReader reader = new FileReader("D:\\problem.txt");
        BufferedReader br = new BufferedReader(reader);
        StringBuffer buffer = new StringBuffer();
        if (br.ready()){
            String temp ;
            while ((temp= br.readLine())!=null){
                buffer.append(temp);
                buffer.append("\n");
            }
            System.out.println(buffer.toString());
        }else {
            System.out.println("还未有过记录");
        }
    }
    //打印历史成绩
    public void printAllData() throws IOException {
        FileReader reader = new FileReader("D:\\record.txt");
        BufferedReader bufferedReader = new BufferedReader(reader);
        StringBuffer buffer = new StringBuffer();
        if (bufferedReader.ready()){
            String temp;
            while ((temp=bufferedReader.readLine())!=null){
                buffer.append(temp);
                buffer.append("\n");
            }
            System.out.println(buffer.toString());
        }else {
            System.out.println("还未有记录");
        }
    }
}
